package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Report;
import com.itheima.entity.Result;
import com.itheima.service.ReportService_Lizekai;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController_Lizekai {
    @Reference
    private ReportService_Lizekai reportService;
    @RequestMapping("/getOrderReport")
    public Result getOrderReport(String start, String last) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(start));
        List<String> dateList = new ArrayList<>();
        List<Integer> orderCountList = new ArrayList<>();
        List<Integer> orderVisitCountList = new ArrayList<>();
        for (long d = cal.getTimeInMillis(); d <= sdf.parse(last).getTime(); d = get_D_Plaus_1(cal)) {
            String format = sdf.format(d);
            int orderCount = reportService.orderCount(format);
            int orderVisitCount = reportService.orderVisitCount(format);
            orderCountList.add(orderCount);
            orderVisitCountList.add(orderVisitCount);
            dateList.add(format);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("days",dateList);
        map.put("dayCount",orderCountList);
        map.put("dayCount1",orderVisitCountList);
        return new Result(true,"查询预约数据成功",map);
    }
    @RequestMapping("/getSetmealPriceReport")
    public Result getSetmealPriceReport(String start,String last){
        List<Map<String,Object>> list = reportService.getSetmealPriceReport(start,last);
        return new Result(true,"查询统计结果成功",list);
    }
    @RequestMapping("/getMemberAgeReport")
    public Result getMemberAgeReport(){
        List<Map<String,Object>> list = null;
        try {
            list = reportService.getMemberAgeReport();
            return new Result(true,"查询统计结果成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询统计结果失败");
        }
    }
    @RequestMapping("/getMemberSexReport")
    public Report getMemberSexReport(){
        Report report = reportService.getMemberSexReport();
        return report;
    }
    public static long get_D_Plaus_1(Calendar c){
        c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) + 1);
        return c.getTimeInMillis();
    }
}
